from genericpath import exists, isdir
import os
import sys
from typing import Match
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QLineEdit, QMainWindow, QMessageBox, QPushButton, QTextEdit
from Ui_mainWindow import *
from ImplantedSDK import *
import Log
#import webbrowser    #超链接

__Author__ = "By: Geek7\nEmail: chenjiechao666@gmail.com"
__Copyright__ = "Copyright (c) 2021 Geek7.\"[极客七]"
__Version__ = "Version 1.0"

 
class MainWindow(QMainWindow, Ui_MainWindow):

    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)
        self.setFixedSize(self.width(), self.height())
        self.input_pkt.textChanged.connect(self.editchange)
        self.btn_apply.clicked.connect(self.apply)
        self.slider_sdk.valueChanged.connect(self.slider_sdk_change)
        self.slider_u3d.valueChanged.connect(self.slider_u3d_change)

        # 超链接案例
        #self.btn_help.clicked.connect(lambda:webbrowser.open_new("https://badapple001.github.io/fastImportGA/"))

    def slider_sdk_change(self):
        cur = self.slider_sdk.value()
        maxV = self.slider_sdk.maximum()
        
        vers = ["30","31","32"]
        progress = round(cur / maxV * (len(vers)-1))
        self.label_sdk.setText(vers[progress])
        
    def slider_u3d_change(self):
        cur = self.slider_u3d.value()
        maxV = self.slider_u3d.maximum()

        vers = ["2019.x","2020.x","2021.x"]
        progress = round(cur / maxV * (len(vers)-1))
        self.label_u3d.setText(vers[progress])

    lastUrl = None
    yes = False
    def editchange(self):
        src = self.input_pkt.toPlainText()
        if 'file:///' in src:
            src = src.replace('file:///', '',1)
            if self.lastUrl and self.lastUrl in src:
                src = src.replace(self.lastUrl,'',1)
            self.lastUrl = src
            self.input_pkt.setText(src)

            _isDir = os.path.isdir(src)
            existAssetsDir = os.path.exists(f"{src}/unityLibrary")
            self.yes = True
            if not ( _isDir and existAssetsDir ):
                QMessageBox.warning(self, '目录错误!', 'Unity Android Export 目录错误\n请确认!',QMessageBox.Yes)
                self.yes = False
    def apply(self):

        if not self.yes:
            QMessageBox.warning(self, '目录错误!', 'Unity Android Export 目录错误\n请确认!',QMessageBox.Yes)
            return

        Log.Print("apply")
        self.btn_apply.setEnabled(False)

        gameKey = self.input_gk.text()
        gameSecret = self.input_gs.text()
        apptoken = self.input_at.text()
        url = self.input_pkt.toPlainText()

        
        #<font color='#00ff00'>
        outContent = f"""
=========本次接入SDK重要信息==========

项目目录: \"{url}\"
gameKey: {gameKey}
gameSecret: {gameSecret}
Apptoken: {apptoken}
UnityVersion: {self.label_u3d.text()}
SDKVersion: {self.label_sdk.text()}

=========本次接入SDK重要信息==========
"""
        reply = QMessageBox.question(self, '请确认!', outContent,QMessageBox.Yes|QMessageBox.No)
        if reply == QMessageBox.No:
            Log.Print("取消本次导入GA")
            self.btn_apply.setEnabled(True)
        else:
            Log.Print("本次导入GA已验证")
            build(url,gameKey,gameSecret,apptoken,self.label_u3d.text(),self.label_sdk.text(),self.buildComplete)

    def buildComplete(self,state=True):
        QMessageBox.question(self, '提示', "导入完成!!!" if state else "导入失败!!!",QMessageBox.Yes)
        self.btn_apply.setEnabled(True)

        # if state:
        #     QMessageBox.question(None, '提示', "已生成jsbHelper.ts文件!!!",QMessageBox.Yes)
        #     Utils.revealInFileExplorer(os.getcwd())






if __name__ == '__main__':
    app = QApplication(sys.argv)
    myWin = MainWindow()
    myWin.show()
    sys.exit(app.exec_())
    #Log.ExportLog()